package com.netty.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netty.admin.mapper.FriendRelationMapper;
import com.netty.admin.mapper.GroupRelationMapper;
import com.netty.admin.mapper.UserMapper;
import com.netty.admin.security.service.TokenService;
import com.netty.admin.service.FriendRelationService;
import com.netty.admin.service.GroupRelationService;
import com.netty.common.entity.Friend;
import com.netty.common.entity.FriendRelation;
import com.netty.common.entity.GroupRelation;
import com.netty.common.entity.User;
import com.netty.common.entity.bo.FriendFormBo;
import com.netty.common.entity.result.Result;
import com.netty.common.enums.ExceptionEnum;
import com.netty.common.util.ServletUtils;
import com.netty.common.util.security.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author LiuXiaoYu
 * @date 2022/2/1- 13:33
 */
@Service
public class FriendRelationServiceImpl extends ServiceImpl<FriendRelationMapper, FriendRelation> implements FriendRelationService {

    @Autowired
    private FriendRelationMapper friendRelationMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> insert(FriendFormBo friendBo) {
        String username = SecurityUtils.getUsername();
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (Objects.isNull(user)){
            return Result.error(ExceptionEnum.NEED_LOGIN,"请先登录");
        }
        User friend= userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserId,friendBo.getUserId()).eq(User::getUsername, friendBo.getNickname()));
        if (Objects.isNull(friend)){
            return Result.error(ExceptionEnum.ERROR,"该用户不存在");
        }
        FriendRelation friendRelation=new FriendRelation();
        friendRelation.setFriendId(friend.getUserId())
        .setOwnerId(user.getUserId());
        friendRelationMapper.insert(friendRelation);
        //不需要同意让对方也有你这个好友
        FriendRelation reverseFriendRelation=new FriendRelation();
        reverseFriendRelation.setFriendId(user.getUserId())
                .setOwnerId(friend.getUserId());
        friendRelationMapper.insert(reverseFriendRelation);
        return Result.success("添加朋友成功");
    }

    /**
     * 获取该登录用户的所有朋友
     * @return
     */
    @Override
    public Result<?> getAll() {
        String username = SecurityUtils.getUsername();
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        if (Objects.isNull(user)){
            return Result.error(ExceptionEnum.NEED_LOGIN,"请先登录");
        }
        List<FriendRelation> friendRelations = friendRelationMapper.selectList(new LambdaQueryWrapper<FriendRelation>()
                .eq(FriendRelation::getOwnerId, user.getUserId()));
        List<User> friends = null;
        if (friendRelations.size()>0){
            List<Integer> friendIds = friendRelations.stream().map(FriendRelation::getFriendId).collect(Collectors.toList());
            //根据朋友id集合查询他们的用户信息
            friends = userMapper.selectBatchIds(friendIds);
        }
        return Result.success(friends);
    }

    /**
     * 双向删除好友
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteFriend(Integer userId, Integer friendId) {
        //双向删除
        int delete = friendRelationMapper.delete(new LambdaQueryWrapper<FriendRelation>().eq(FriendRelation::getOwnerId, userId).eq(FriendRelation::getFriendId, friendId));
        int delete1 = friendRelationMapper.delete(new LambdaQueryWrapper<FriendRelation>().eq(FriendRelation::getOwnerId, friendId).eq(FriendRelation::getFriendId, userId));
        if (delete>0&&delete1>0){
            //删除成功
            return Result.success("删除成功");
        }
        return Result.error(ExceptionEnum.ERROR,"删除失败");
    }
}
